#include "excelfile.h"
#include "xlsxdocument.h"


ExcelFile::ExcelFile()
{

}


bool ExcelFile::load(const std::string& filepath, tv::FrequenceList& freqList)
{
    QXlsx::Document xlsx(filepath.c_str());
	
	QXlsx::CellRange cellRange = xlsx.dimension();

	if (cellRange.columnCount() < 3)
	{
		return false;
	}

	freqList.clear();

	int row = cellRange.firstRow() + 1;
    for (; row <= cellRange.lastRow(); ++row)
	{
		int col = 1;
		QString num = xlsx.read(row, col++).toString();
		QString name = xlsx.read(row, col++).toString();
		QString memo = xlsx.read(row, col++).toString();

		tv::Frequence freq;
		freq.num = num.toStdString();
		freq.name = name.toStdString();
		freq.memo = memo.toStdString();

		freqList.push_back(freq);
	}

    return true;
}

bool ExcelFile::save(const tv::FrequenceList& freqList, const std::string& filepath)
{
    QXlsx::Document xlsx;
    xlsx.write("A1", "频点编号");
    xlsx.write("B1", "频点名称");
    xlsx.write("C1", "备注");

	for (size_t row = 0; row < freqList.size(); row++)
	{
		int col = 1;
		const tv::Frequence& freq = freqList[row];
		xlsx.write(row + 2, col ++, freq.num.c_str());
		xlsx.write(row + 2, col ++, freq.name.c_str());
		xlsx.write(row + 2, col++, freq.memo.c_str());
	}

    return xlsx.saveAs(filepath.c_str());
}

bool ExcelFile::load(const std::string& filepath, tv::ProgramList& programList)
{
    QXlsx::Document xlsx(filepath.c_str());

    QXlsx::CellRange cellRange = xlsx.dimension();

    if (cellRange.columnCount() < 3)
    {
        return false;
    }

    programList.clear();

    int row = cellRange.firstRow() + 1;
    for (; row <= cellRange.lastRow(); ++row)
    {
        int col = 1;
        QString num = xlsx.read(row, col++).toString();
        QString name = xlsx.read(row, col++).toString();
        QString freqNum = xlsx.read(row, col++).toString();
        QString freqName = xlsx.read(row, col++).toString();
        QString url = xlsx.read(row, col++).toString();
        QString memo = xlsx.read(row, col++).toString();

        tv::Program program;
        program.num = num.toStdString();
        program.name = name.toStdString();
        program.memo = memo.toStdString();
        program.freqNum = freqNum.toStdString();
        program.freqName = freqName.toStdString();
        program.url = url.toStdString();

        programList.push_back(program);
    }

    return true;
}

bool ExcelFile::save(const tv::ProgramList& programList, const std::string& filepath)
{
    QXlsx::Document xlsx;
    xlsx.write("A1", "节目编号");
    xlsx.write("B1", "节目名称");
    xlsx.write("C1", "频点编号");
    xlsx.write("D1", "频点名称");
    xlsx.write("E1", "播放地址");
    xlsx.write("F1", "备注");

	for (size_t row = 0; row < programList.size(); row++)
	{
		int col = 1;
		const tv::Program& program = programList[row];
		xlsx.write(row + 2, col++, program.num.c_str());
		xlsx.write(row + 2, col++, program.name.c_str());
		xlsx.write(row + 2, col++, program.freqNum.c_str());
		xlsx.write(row + 2, col++, program.freqName.c_str());
		xlsx.write(row + 2, col++, program.url.c_str());
		xlsx.write(row + 2, col++, program.memo.c_str());
	}

    return xlsx.saveAs(filepath.c_str());
}
